const app = getApp();
const common = require('../../utils/common.js');
var that = "";

//时间控件
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    roomId:'',//房间ID
    roomName:'',
    listData: [],
    totalfee: 0,
    dete: "",
    //以下为时间选择变量
    years: years,
    year: date.getFullYear(),
    months: months,
    month: date.getMonth() + 1,
    days: days,
    day: date.getDate(),
    value: [9998, 0, 0],
    modalHidden: true,//默认隐藏moal
    page:1,
    manageText:'管理',
    manageB:false,
  },
//统计
  initData: function () {
    common._get('/wx/room/charge/list', { roomId: this.data.roomId, page: this.data.page, limit:15},
      //成功
      function (res) {
        if (res.data.success) {
          console.log(res.data)
          that.setData({
            listData: that.data.listData.concat(res.data.data.list),
          })
          for (var i in that.data.listData) {
            that.data.listData[i].falg=false;
          }
          that.data.page++
        }
      });
  },

  //点击管理按钮
  manageClick:function(){
    if(this.data.manageB){
      this.setData({
        manageText:'管理',
        manageB:false
      })
    }else{
      this.setData({
        manageText: '取消',
        manageB: true
      })
    }
  },
  //统计数据
  
  getTotal: function () {
    common._get('/wx/room/charge/total', { roomId: this.data.roomId },
      //成功
      function (res) {
        console.log(res.data);
        if (res.data.success) {
          that.setData({
            totalfee: res.data.data.totalfee
          })
        }
      });
  },
  /**
   * 点击每一项费用
   */
  chargesItemClick: function (e) {
    var chargeid = e.currentTarget.dataset.chargeid
    wx.navigateTo({
      url: '../receiptDetails/receiptDetails?chargeid=' + chargeid,
    })
  },
  //点击选择费用项
  chargesSelectItemClick:function (e) {
    var index = e.currentTarget.dataset.index
    var pos=0;
    // for(var i=0;i<=index;i++ ){
    //   if (this.data.listData[i].ischarged!=0){
    //     pos=pos+1;
    //   }
    // }
    console.log('index' + index+'pos'+pos);
    this.data.listData[index + pos].flag = !this.data.listData[index + pos].flag
    this.setData({
      listData: this.data.listData
    })

  },
  //时间modal改变监听
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
    })
  },
  //显示
  showModel: function () {

    // that.setData({
    //   modalHidden: false,
    // })
  },
  //modal确认取消
  //确认
  confirm: function () {
    // this.setData({
    //   modalHidden: true,
    // })
    // this.setMyDeta();
    // this.initData(that.data.year + '-' + that.data.month + '-1');
  },
  //取消
  cancel: function () {
    this.setData({
      modalHidden: true,
    })
  },
  //设置时间绑定
  setMyDeta: function () {
    that.setData({
      dete: that.data.year + '年' + that.data.month + '月'
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    if (options.room_id){
      this.setData({
        roomId: options.room_id,
        roomName: options.room_name
      })
    }
    that.getTotal();
    that.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.getRefresh()) {
      this.data.page = 1;
      that.data.listData = [];
      that.getTotal();
      that.initData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.page=1;
    that.data.listData=[];
    that.getTotal();
    that.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    that.getTotal();
    that.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})